package org.fing.edu.uy.admin.config;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MgrAdminConfig {
	
	private static MgrAdminConfig instance;
	private Properties secure_config_prop;
	private Properties meta_suggest_prop;
	private Properties mbean_conection;

	private MgrAdminConfig(){
		try{
			InputStream in = this.getClass().getClassLoader().getResourceAsStream("secure-login.properties");
			secure_config_prop = new Properties();
			secure_config_prop.load(in);
		}catch (Exception ex) {
			Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, "Error reading file secure-login.properties. See Trace.", ex);
		}
		
		try{
			InputStream in = this.getClass().getClassLoader().getResourceAsStream("meta-suggest.properties");
			meta_suggest_prop = new Properties();
			meta_suggest_prop.load(in);
		}catch (Exception ex) {
			Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, "Error reading file meta-suggest.properties. See Trace.", ex);
		}
		
		try{
			InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties");
			mbean_conection = new Properties();
			mbean_conection.load(in);
		}catch (Exception ex) {
			Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, "Error reading file config.properties. See Trace.", ex);
		}
	}
	
	public static synchronized MgrAdminConfig getInstance(){
		if(instance == null){
			instance = new MgrAdminConfig();
		}
		return instance;
	}
	
	public String getSecureProperty(String key, String defaultValue){
		if(secure_config_prop != null){
			return secure_config_prop.getProperty(key, defaultValue);
		}else{
			return defaultValue;
		}
	}
	
	public List<String> getMetaSuggest(){
		List<String> suggest = new LinkedList<String>();
		for(Entry<Object, Object> entry : meta_suggest_prop.entrySet()){
			suggest.add(entry.getValue().toString());
		}
		return suggest;
	}
	
	public String getMBeanProperty(String key){
		if(mbean_conection != null){
			return mbean_conection.getProperty(key);
		}else{
			return "";
		}
	}
	
}
